const axios = require('axios');

// 配置
const BASE_URL = 'http://localhost:3000/api';

// 格式化章节信息
function formatChapters(chapters) {
  if (!chapters || chapters.length === 0) {
    return '暂无章节';
  }
  
  return chapters.map((chapter, index) => {
    return `  ${index + 1}. ${chapter.title} (第${chapter.sort}章)
    ⏱️ 时长: ${chapter.duration || 0}分钟
    💎 积分: ${chapter.points}
    👁️ 观看: ${chapter.viewCount}次
    📅 创建: ${new Date(chapter.createdAt).toLocaleDateString()}`;
  }).join('\n');
}

// 格式化短剧信息
function formatShort(short, index) {
  return `📺 ${index + 1}. ${short.title}
  📝 简介: ${short.summary || '暂无简介'}
  🎬 导演: ${short.instructor}
  📊 观看: ${short.viewCount}次 | 点赞: ${short.likeCount}次 | 收藏: ${short.favoriteCount}次
  🏷️ 分类: ${short.category?.name || '未分类'} | 方向: ${short.direction?.name || '未分类'}
  💰 免费: ${short.isFree ? '是' : '否'} | 状态: ${short.status}
  📚 章节数: ${short.episodeCount} | 总时长: ${short.totalDuration}分钟
  📅 创建: ${new Date(short.createdAt).toLocaleDateString()}
  
📖 章节列表:
${formatChapters(short.chapters)}
${'─'.repeat(50)}`;
}

// 测试获取随机短剧（详细展示）
async function testGetRandomShortsDetailed() {
  try {
    console.log('🎲 获取随机短剧（包含完整章节信息）...\n');
    
    const response = await axios.get(`${BASE_URL}/shorts/random?count=3`);
    
    if (response.data && response.data.length > 0) {
      console.log(`✅ 成功获取 ${response.data.length} 个随机短剧\n`);
      
      response.data.forEach((short, index) => {
        console.log(formatShort(short, index));
        console.log('');
      });
      
      // 统计信息
      const totalChapters = response.data.reduce((sum, short) => sum + (short.chapters?.length || 0), 0);
      const freeShorts = response.data.filter(short => short.isFree).length;
      const completedShorts = response.data.filter(short => short.status === 'COMPLETED').length;
      
      console.log('📊 统计信息:');
      console.log(`  总短剧数: ${response.data.length}`);
      console.log(`  总章节数: ${totalChapters}`);
      console.log(`  免费短剧: ${freeShorts}`);
      console.log(`  已完结: ${completedShorts}`);
      
    } else {
      console.log('❌ 未获取到短剧数据');
    }
    
    return response.data;
  } catch (error) {
    console.error('❌ 获取随机短剧失败:', error.response?.data || error.message);
  }
}

// 测试获取指定分类的随机短剧
async function testGetRandomShortsByCategory(categoryId = 1, count = 2) {
  try {
    console.log(`🎲 获取分类${categoryId}的${count}个随机短剧...\n`);
    
    const response = await axios.get(`${BASE_URL}/shorts/random?categoryId=${categoryId}&count=${count}`);
    
    if (response.data && response.data.length > 0) {
      console.log(`✅ 分类${categoryId}的${count}个随机短剧:\n`);
      
      response.data.forEach((short, index) => {
        console.log(formatShort(short, index));
        console.log('');
      });
    } else {
      console.log(`❌ 分类${categoryId}中未找到短剧`);
    }
    
    return response.data;
  } catch (error) {
    console.error('❌ 获取分类随机短剧失败:', error.response?.data || error.message);
  }
}

// 测试获取免费随机短剧
async function testGetRandomFreeShorts(count = 2) {
  try {
    console.log(`🎲 获取${count}个免费随机短剧...\n`);
    
    const response = await axios.get(`${BASE_URL}/shorts/random?isFree=true&count=${count}`);
    
    if (response.data && response.data.length > 0) {
      console.log(`✅ ${count}个免费随机短剧:\n`);
      
      response.data.forEach((short, index) => {
        console.log(formatShort(short, index));
        console.log('');
      });
    } else {
      console.log('❌ 未找到免费短剧');
    }
    
    return response.data;
  } catch (error) {
    console.error('❌ 获取免费随机短剧失败:', error.response?.data || error.message);
  }
}

// 主测试函数
async function runDetailedTests() {
  console.log('🚀 开始详细测试随机短剧接口...\n');
  console.log('='.repeat(60));

  // 1. 获取默认随机短剧（3个）
  await testGetRandomShortsDetailed();
  console.log('\n' + '='.repeat(60));

  // 2. 获取指定分类的随机短剧
  await testGetRandomShortsByCategory(1, 2);
  console.log('\n' + '='.repeat(60));

  // 3. 获取免费随机短剧
  await testGetRandomFreeShorts(2);
  console.log('\n' + '='.repeat(60));

  console.log('✨ 详细测试完成！');
}

// 运行测试
runDetailedTests().catch(console.error); 